package com.seari.vehbaseF.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.seari.vehbaseF.entity.DeviceInfo;
import com.seari.vehbaseF.mapper.DeviceInfoMapper;
import com.seari.vehbaseF.service.DeviceInfoService;
import com.seari.vehbaseF.util.ExcelUtils;
import com.seari.vehbaseF.util.HttpClientUtil;
import com.seari.vehbaseF.util.Utlis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service("DeviceInfoService")
public class DeviceInfoServiceimpl implements DeviceInfoService {


    @Autowired
    public DeviceInfoMapper deviceInfoMapper;

    @Override
    public List<DeviceInfo> seachDeviceInfo(String ProjectCode) {
        return deviceInfoMapper.seachDeviceInfo(ProjectCode);
    }

    @Override
    public List<DeviceInfo> seachDeviceInfoBySystem(String ProjectCode, String SystemCode) {

        return deviceInfoMapper.seachDeviceInfoBySystem(ProjectCode,SystemCode);
    }

    @Override
    public void addDeviceInfoList() throws IOException, ParseException {
        InitAllDevice();
    }

    public void InitAllDevice() throws IOException,ParseException {

        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(date);

        List<DeviceInfo> resList = new ArrayList<>();

        //先调接口  初始化  获取  所有基地信息
        HttpClientUtil clientUtil = HttpClientUtil.getInstance();
        String project  = clientUtil.sendHttpGet("http://webapi.eegrid.com/project/get?accessToken=c2a1aea4d43077b90e77588f76379abc");
        Map<String,Object> projectMap = (Map<String,Object>) JSONObject.parse(project);
        List<Map<String, Object>> projectList = (List<Map<String, Object>>) projectMap.get("data");

        //获取所有系统信息
        String system = clientUtil.sendHttpPost("http://webapi.eegrid.com/devicesystem/get?accessToken=c2a1aea4d43077b90e77588f76379abc");
        Map<String,Object> systemdata = (Map<String, Object>) JSONObject.parse(system);
        List<Map<String, Object>> Systemlist = (List<Map<String, Object>>) systemdata.get("data");
        Map<Integer, String> systemap = Utlis.ListToMap(Systemlist);

        //获取所有设备类型
        String devtype = clientUtil.sendHttpPost("http://webapi.eegrid.com/devicetype/get?accessToken=c2a1aea4d43077b90e77588f76379abc");
        JSONObject jsonObjectsystemtype = JSONObject.parseObject(devtype);
        JSONArray jsondevArray = jsonObjectsystemtype.getJSONArray("data");

        Map<String,Object> devtypedata = (Map<String, Object>) JSONObject.parse(devtype);
        List<Map<String, Object>> devtypelist = (List<Map<String, Object>>) devtypedata.get("data");
        Map<Integer, Integer> devtypemap = Utlis.TypeListToMap(devtypelist);
        //创建一个DeviceTypeID与SystemCode对应的map
        Map<Integer, Integer> devsysemap = Utlis.DeviceSystemListToMap(devtypelist);


        for (Map<String,Object> pro : projectList) {
            //获取到所有projectCode
            Integer projectCode = (Integer) pro.get("projectCode");
            //根据projectcode获取下面对应的所有设备实体
            Map<String,String> map = new HashMap<String,String>();
            map.put("projectcode",String.valueOf(projectCode));
            String devs = clientUtil.sendHttpPost("http://webapi.eegrid.com/device/get?accessToken=c2a1aea4d43077b90e77588f76379abc", map);
            JSONObject jsonObjectsystemproject = JSONObject.parseObject(devs);
            JSONArray jsonArray = jsonObjectsystemproject.getJSONArray("data");

            for (int i = 0; i < jsonArray.size(); i++) {
                DeviceInfo deviceInfo = JSONObject.parseObject(String.valueOf(jsonArray.getJSONObject(i)), DeviceInfo.class);
                deviceInfo.setProjectCode(Integer.toString(projectCode));
                deviceInfo.setGmtCreate(dateString);
                deviceInfo.setGmtModified(dateString);
                resList.add(deviceInfo);
            }

        }

        //设备主体关联
        for (DeviceInfo deviceInfo : resList) {

            for (int j = 0; j <jsondevArray.size() ; j++) {
                JSONObject jsonj = jsondevArray.getJSONObject(j);
                //根据DeviceTypeID对应找TypeId
                if (jsonj.getIntValue("DeviceTypeID")==deviceInfo.getTypeId()&&jsonj.getString("SystemCode")!=null){
                    deviceInfo.setSystemCode(jsonj.getString("SystemCode"));
                    deviceInfo.setSystemName(systemap.get(Integer.valueOf(deviceInfo.getSystemCode())));
                }//如果找不到SystemCode
                if (jsonj.getIntValue("DeviceTypeID")==deviceInfo.getTypeId()&&jsonj.getString("SystemCode")==null&&jsonj.getString("ParentCode")!=null){
                    Integer DeviceTypeID = devtypemap.get(jsonj.getInteger("ParentCode"));
                    Integer syscode =  devsysemap.get(DeviceTypeID);
                    if (syscode!=null){
                        deviceInfo.setSystemCode(Integer.toString(syscode));
                        deviceInfo.setSystemName(systemap.get(syscode));
                    }
                }
            }
        }

        deviceInfoMapper.deleteOldDevice();
        deviceInfoMapper.addDeviceInfoList(resList);

    }
}
